#include "simpleCounter.h"



// Forward declaration
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

// Main function
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lCmdLine, int nCmdShow)
{
// make the window
	// SOME VARIABLES
	WNDCLASS wc;	// Window class
	HWND hwnd;		// Handle to the window
	MSG msg;
	
	
	const LPCTSTR szClassName = TEXT("simpleCounter");
	const LPCTSTR szWndName = TEXT("Simple Counter!");	// for this window
	wc.style = CS_VREDRAW | CS_HREDRAW ;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInst;
	wc.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_MYICON));
	wc.hCursor = LoadCursor(NULL,IDC_HAND);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 3);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = szClassName;

	if(!RegisterClass(&wc))
	{
		MessageBox(NULL,TEXT("Initialization failed."),TEXT("Error"),MB_OK);
		return 0;
	}
	hwnd = CreateWindow(
						szClassName,
						szWndName,
						WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT,
						CW_USEDEFAULT,
						700,
						500,
						NULL,
						NULL,
						hInst,
						NULL);
	if(hwnd == NULL)
	{
		MessageBox(NULL,TEXT("Failed to initialization."),TEXT("Error"),MB_OK);
		return 0;
	}
	
	
	ShowWindow(hwnd,SW_MAXIMIZE);
	UpdateWindow(hwnd);

	while(GetMessage(&msg,hwnd,NULL,NULL) > 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)(msg.wParam);
} // End of Main function

// Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg, WPARAM	wParam, LPARAM lParam)
{
	static int counter = 0;
	WCHAR sCounter[sizeof(int)+1];
	_itow_s(counter,sCounter,sizeof(int)+1,10);
	HFONT hFont = NULL;
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;
	HWND hwndButton;
	RECT wRect;
	int _w;
	int _h;
	GetClientRect(hwnd,&wRect);
	_h = wRect.bottom - wRect.top;
	_w = wRect.right - wRect.left;

	switch(msg)
	{
	case WM_CREATE:

			hwndButton = CreateWindow(
			L"BUTTON",
			L"",
			WS_CHILD|WS_VISIBLE|BS_FLAT,
			(_h-80),
			(_w-160),
			80,
			40,
			hwnd,
			(HMENU)IDC_MAIN_BUTTON,
			(HINSTANCE)GetWindowLongW(hwnd,GWL_HINSTANCE),
			NULL);
	
			SendMessage(hwndButton,WM_SETTEXT,NULL,(LPARAM)TEXT("Clear"));
		break;
	case WM_PAINT:
		//DO SOMTHING		
		GetClientRect(hwnd,&rect);
		hdc = BeginPaint(hwnd,&ps);
		hFont = CreateFont(200, 100, 2, 0, FW_BOLD, false, false, false, ANSI_CHARSET,
						OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
						DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"));
		SelectObject(hdc,hFont);
		SetBkMode(hdc,TRANSPARENT);;
		SetTextColor(hdc,RGB(255,255,255));
		DrawText(hdc,sCounter,-1,&rect,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		EndPaint(hwnd,&ps);
		break;
	case WM_DESTROY:
		DeleteObject(hFont);
		PostQuitMessage(0);
		break;
	case WM_LBUTTONDOWN:
		MessageBeep((UINT)(-1));
		++counter;
		RedrawWindow(hwnd,NULL,0,RDW_UPDATENOW | RDW_INVALIDATE | RDW_ERASE);
		break;
	case WM_SIZE:
		RedrawWindow(hwnd,NULL,0,RDW_UPDATENOW | RDW_INVALIDATE | RDW_ERASE);
		break;
	case WM_KEYDOWN:
		switch (wParam)
		{
		case 0X20:
			++counter;
		    RedrawWindow(hwnd,NULL,0,RDW_UPDATENOW | RDW_INVALIDATE | RDW_ERASE);
			break;
		case 0xD:
			++counter;
		    RedrawWindow(hwnd,NULL,0,RDW_UPDATENOW | RDW_INVALIDATE | RDW_ERASE);
			break;
		default:
			break;
		}	
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam)){
		case IDC_MAIN_BUTTON:
			counter = 0;
			RedrawWindow(hwnd,NULL,0,RDW_UPDATENOW | RDW_INVALIDATE | RDW_ERASE);
			SetFocus(hwnd);
			break;
		}
		break;
	default:
		return DefWindowProc(hwnd,msg,wParam,lParam);
	}
	return 0;
}

